package com.lyshop.jinding.activitys;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.lyshop.jinding.R;
import com.lyshop.jinding.adapters.FollowOrderAdapter;
import com.lyshop.jinding.base.BaseActivity;
import com.lyshop.jinding.bean.FollowOrderBean;
import com.lyshop.jinding.bean.FollowOrderRequestBean;
import com.lyshop.jinding.eventbus.EventCenter;
import com.lyshop.jinding.eventbus.EventConstants;
import com.lyshop.jinding.net.ActionCallbackListener;
import com.lyshop.jinding.utils.DataUtils;
import com.lyshop.jinding.utils.ToastUtil;
import com.lyshop.jinding.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class MyFollowOrderActivity extends BaseActivity {
    private SmartRefreshLayout refresh_layout;
    private RecyclerView rv_list;
    private TextView tv_empty;
    private List<FollowOrderBean> mFollowOrderList;
    private FollowOrderRequestBean mRequestBean;
    private FollowOrderAdapter followOrderAdapter;
    private int mPageIndex = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_my_follow_order;
    }

    @Override
    protected void findViews() {
        refresh_layout = findViewById(R.id.refresh_layout);
        rv_list = findViewById(R.id.rv_list);
        tv_empty = findViewById(R.id.tv_empty);
    }

    @Override
    protected void setListeners() {
        refresh_layout.setEnableLoadMore(true);
        refresh_layout.setEnableRefresh(true);

        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPageIndex++;
                getMyFollowList();
            }
        });

        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPageIndex = 1;
                getMyFollowList();
            }
        });
    }

    private void initData() {
        mRequestBean = new FollowOrderRequestBean();
        mFollowOrderList = new ArrayList<>();

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        rv_list.setLayoutManager(layoutManager);
        followOrderAdapter = new FollowOrderAdapter();
        rv_list.setAdapter(followOrderAdapter);
        getMyFollowList();
    }

    private void getMyFollowList() {
        String userId = DataUtils.getUserId(mContext);
        mAppAction.getMyFollowOrder(mPageIndex, userId, new ActionCallbackListener<List<FollowOrderBean>>() {
            @Override
            public void onSuccess(List<FollowOrderBean> data) {
                completeRefresh();

                if (mPageIndex == 1){
                    if (data != null && data.size() > 0){
                        mFollowOrderList = data;
                    }else {
                        mFollowOrderList.clear();
                    }
                }else {
                    if (data != null && data.size() > 0){
                        mFollowOrderList.addAll(data);
                    }
                }

                if (data.size() == 10) {
                    refresh_layout.setEnableLoadMore(true);
                } else {
                    refresh_layout.setEnableLoadMore(false);
                }

                if (mFollowOrderList.size() > 0) {
                    rv_list.setVisibility(View.VISIBLE);
                    tv_empty.setVisibility(View.GONE);
                    updateOrderList();
                } else {
                    rv_list.setVisibility(View.GONE);
                    tv_empty.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onFailure(String errorCode, String message) {
                completeRefresh();
                rv_list.setVisibility(View.GONE);
                tv_empty.setVisibility(View.VISIBLE);
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void updateOrderList() {
        followOrderAdapter.setDataList(Utils.completionFollow(mFollowOrderList));
    }

    /**
     * 关闭刷新加载
     */
    private void completeRefresh() {
        refresh_layout.finishRefresh(1000);
        refresh_layout.finishLoadMore(1000);
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        super.onEventComming(eventCenter);
        int event = eventCenter.getEventCode();
        switch (event) {
            case EventConstants.EVENTCODE_UPDATE_PRO_PRICE:
                updateOrderList();
                break;
        }
    }
}